package com.xingda.packing.dmds.minitor.controller;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingda.packing.dmds.common.BaseController;
import com.xingda.packing.dmds.common.R;
import com.xingda.packing.dmds.common.dto.PageDto;
import com.xingda.packing.dmds.minitor.entity.PkEvents;
import com.xingda.packing.dmds.minitor.service.IPkEventsService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@RestController("pkEventsDMDSController")
@RequestMapping("/api/event")
public class PkEventsController extends BaseController {

    @Resource
    private IPkEventsService pkEventsService;

    @GetMapping("/getList")
    public R getList(Integer size, Integer area) {
        LambdaQueryChainWrapper<PkEvents> pkEventChainWrapper = pkEventsService.lambdaQuery().orderByDesc(PkEvents::getCreateTime);
        pkEventChainWrapper.eq(PkEvents::getArea, area);
        if (Objects.isNull(size) || size < 0) {
            size = 50;
        }
        List<PkEvents> events = pkEventChainWrapper.last(size > 0, "limit 0," + size).list();
        return R.ok().put("data", events);
    }

    @GetMapping("/getPage")
    public R getPage(PageDto dto) {
        Page<PkEvents> pkEventsPage = this.pkEventsService.getPkEventsPage(dto);
        return R.ok().put("data", pkEventsPage);
    }
}
